/*
* @Author: hzwlxy
* @Email: 120235331@qq.com
* @Github: http：//www.github.com/siaoynli
* @Date: 2019/6/28 11:06
* @Version:
* @Description: 管理员登录
*/

let LeeLoginPage = function () {

    var loading=$('<div class="lee-loading"><i class="iconfont icon-loading"></i></div>');

    //获取时间
    function getNewTime() {
        var date = new Date();
        var newhour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var newminute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        var newsecond = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        var newtime = newhour + ":" + newminute + ":" + newsecond;
        $(".lee-time").html(newtime);
        date = null;
    }

    function initDateTime() {
        var myDate = new Date();
        var year = myDate.getFullYear();
        var month = myDate.getMonth() + 1;
        var day = myDate.getDate();
        var newdate = year + "年" + month + "月" + day + "日";
        var week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[myDate.getDay()];
        $(".lee-date").html(newdate);
        $(".lee-week").html(week);
        getNewTime();
        setInterval(getNewTime, 1000);
    }

    function setLogin() {
        $("body").on("click", function () {
            $(".lee-datetime").animate({opacity: 0}, 500, function () {
                $(".lee-datetime").hide();
            });
            $(".lee-login-container").show().animate({opacity: 1}, 500);
        });
    }

    function chooseUser() {
        var avatar=$(".lee-user-avatar").find("img");
        var avatarImg=avatar.attr("src");

        $(".lee-choose-user>ul>li").click(function () {
            $(this).siblings("li").removeClass("lee-active");
            $(this).addClass("lee-active");
            var username=$(".input-username");
            var other= $(this).find("span.username");
            var nicknameTxt =other.html();
            var usernameTxt =other.attr("data-name");

            $(".lee-username").html(nicknameTxt);

            if (usernameTxt === "other") {
                username.find("input").val("");
                $(".input-password").find("input").val("");
                username.show();
                avatar.attr("src",other.siblings("span").find("img").attr("src"))
            } else {
                username.find("input").val(usernameTxt);
                username.hide();
                avatar.attr("src",avatarImg)
            }
        });
    }

    function login() {
        $("button.lee-login-btn").click(function () {
            var usernameTxt= $(".lee-username").html();
            var user_name = $(".input-username").find("input");
            var password = $(".input-password").find("input");
            var form=$("form[name=login]");

            $(".lee-login-form").hide();
            if(user_name.val() === "") {
                showNotice("请输入用户名");
                return;
            }
            if(password.val() === "") {
                showNotice("请输入密码");
                return;
            }
            // if (usernameTxt === "其他用户") {
            //     $(".lee-username").html(user_name.val())
            // }
            $(".lee-login-det").append(loading);

            $.ajax({
                type: 'post',
                url: form.attr("action"),
                data: form.serialize(),
                dataType: 'json',
                timeout: 5000,          // 设置超时时间
                beforeSend:function(){
                    loading.show();
                },
                success: function (result) {
                    if (result.state === 1) {
                         window.location=result.url;
                    } else {
                        showNotice(result.message);
                    }
                },
                error: function (XmlHttpRequest) {
                    showNotice('系统出现错误，请联系管理员');
                },
                complete: function (XMLHttpRequest,status) {
                    if(status === 'timeout') {
                        xhr.abort();    // 超时后中断请求
                        showNotice('请求超时');
                    }
                }
            });
        });
    }


    function showNotice(msg) {
            loading.remove();
            $(".lee-notice-msg").html(msg);
            $(".lee-message-container").show();
    }

    function gotoClock() {
        $(".lee-clock").click(function (event) {
            event.stopPropagation();
            $(".lee-datetime").show().animate({opacity: 1}, 500);
            $(".lee-login-container").animate({opacity: 0}, 500, function () {
                $(".lee-login-container").hide();
            });
        });
    }
    
    function confirmBack() {
        $(".lee-confirm-button>span").click(function () {
                $(".lee-message-container").hide();
                $(".lee-login-form").show();
        })
    }


    //全屏
    function fullScreen() {
        var ele = document.documentElement
            , reqFullScreen = ele.requestFullScreen || ele.webkitRequestFullScreen
            || ele.mozRequestFullScreen || ele.msRequestFullscreen;
        if (typeof reqFullScreen !== 'undefined' && reqFullScreen) {
            reqFullScreen.call(ele);
        }
    };

    //退出全屏
    function exitScreen() {
        if (document.exitFullscreen) {
            document.exitFullscreen();
        } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        } else if (document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen();
        } else if (document.msExitFullscreen) {
            document.msExitFullscreen();
        }
    };

    function setScreen() {
        var obj=$("a.full-screen");
        var title= obj.attr("title");
        var icon= obj.find('i');
        obj.click(function () {
             if(icon.hasClass("icon-quanping")){
                 icon.removeClass("icon-quanping").addClass("icon-quxiaoquanping");
                 fullScreen();
                 $(this).attr("退出全屏");
             }else{
                 exitScreen();
                 icon.removeClass("icon-quxiaoquanping").addClass("icon-quanping");
                 $(this).attr(title);
             }
         })
    };

    function initCsrf() {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
    }

    return {
        init: function () {
            initCsrf();
            initDateTime();
            setLogin();
            chooseUser();
            gotoClock();
            confirmBack();
            setScreen();
            login();
        }
    }

}();


jQuery(document).ready(function () {
    LeeLoginPage.init();
});


